package com.xiaoyu.izf.controller;

import cn.hutool.core.util.StrUtil;
import com.xiaoyu.izf.aspect.RequestAuth;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.model.page.DataTableBean;
import com.xiaoyu.izf.model.page.ParamListBean;
import com.xiaoyu.izf.multivalues.TwoValue;
import com.xiaoyu.izf.service.CommissionLogService;
import com.xiaoyu.izf.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
@RequestMapping("/commission_log")
public class CommissionLogController extends BaseController{

    @Autowired
    private CommissionLogService commissionLogService;
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/par_list_page")
    @RequestAuth
    public String parListPage(HttpServletRequest request, Model model){
        UserInfo user = user(request);
        model.addAttribute("uid",user.getId());
        return "mgr/commission_log/par_list";
    }

    @RequestMapping("/par_list")
    @ResponseBody
    @RequestAuth
    public DataTableBean parList(HttpServletRequest request, @RequestBody String body) throws IOException {
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = commissionLogService.list(paramListBean);

        String strTmp = "付款总和（元）：{}------佣金总和（元）：{}";
        TwoValue<Double,Double> twoValue = commissionLogService.total(paramListBean);
        return dataTableBean.setTotalSpace(StrUtil.format(strTmp,twoValue.getValue1(),twoValue.getValue2()));
    }

    @RequestMapping("/list_page")
    @RequestAuth
    public String listPage(HttpServletRequest request, Model model){
        return "mgr/commission_log/admin_list";
    }

    @RequestMapping("/list")
    @ResponseBody
    @RequestAuth
    public DataTableBean list(HttpServletRequest request, @RequestBody String body) throws IOException {
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = commissionLogService.list(paramListBean);

        String strTmp = "付款总和（元）：{}------佣金总和（元）：{}";
        TwoValue<Double,Double> twoValue = commissionLogService.total(paramListBean);
        return dataTableBean.setTotalSpace(StrUtil.format(strTmp,twoValue.getValue1(),twoValue.getValue2()));
    }

    /**
     * admin用户列表的某个用户的佣金列表
     * @param model
     * @return
     */
    @RequestMapping("/cur_user_list_page")
    @RequestAuth
    public String curUserListPage(Model model,int uid){
        model.addAttribute("uid",uid);
        return "mgr/commission_log/cur_user_list";
    }
    @RequestMapping("/cur_user_list")
    @ResponseBody
    @RequestAuth
    public DataTableBean cur_user_list(HttpServletRequest request, @RequestBody String body) throws IOException {
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = commissionLogService.list(paramListBean);

        String strTmp = "付款总和（元）：{}------佣金总和（元）：{}";
        TwoValue<Double,Double> twoValue = commissionLogService.total(paramListBean);
        return dataTableBean.setTotalSpace(StrUtil.format(strTmp,twoValue.getValue1(),twoValue.getValue2()));
    }
}
